package com.jnuo.mdb

import android.content.Context
import android.content.SharedPreferences
import com.google.gson.reflect.TypeToken

/**
 * Created by zhangyuncai.
 * Date: 2022/9/14
 */
object MdbParamsHelper {
    private val TAG = "MdbParamsHelper"
    lateinit var applicationContext: Context

    @JvmStatic
    fun init(context: Context) {
        applicationContext = context
    }

    private const val GLOBALTEXT = "globalText"
    private val prefs: SharedPreferences by lazy {
        applicationContext.getSharedPreferences("MilkInterval", Context.MODE_PRIVATE)
    }

    private fun <T> getValue(key: String, default: T): T {
//        Log.e(TAG, "key:${key}")
        try {
            val map = getMap()
//            Log.i(TAG, "map$map")
            if (map == null || !map.containsKey(key)) {
                return default
            }
            return map!!.get(key) as T

        } catch (e: Exception) {
            e.printStackTrace()
        }
        return default
    }

    private fun getMap(): HashMap<String, Any>? {
        val json = prefs.getString(GLOBALTEXT, "")
        val type = object : TypeToken<HashMap<String, Any>>() {}.type
        val map = MdbUtil.jsonToAny<HashMap<String, Any>>(json, type)
        return map
    }

    //是否闲时等待
    @JvmStatic
    val mdb_set_always_idle_cashless: String
        get() = when (getValue("mdb_set_always_idle_cashless", "")) {
            "0" -> "0"
            "1" -> "1"
            else -> "-1"
        }

    //货币金额扩展
    @JvmStatic
    val mdb_set_expand_currency_mode_cashless: String
        get() = when (getValue("mdb_set_expand_currency_mode_cashless", "0")) {
            "1" -> "1"
            else -> "0"
        }

    //纸币暂存
    @JvmStatic
    val mdb_set_escrow_bill: String
        get() = when (getValue("mdb_set_escrow_bill", "0")) {
            "0" -> "0"
            else -> "1"
        }

    //拒绝接收的纸币面值
    @JvmStatic
    val mdb_exclude_from_next_amount_bill: ArrayList<Double>
        get() = getValue("mdb_exclude_from_next_amount_bill", arrayListOf())

    //拒绝接收的硬币面值
    @JvmStatic
    val mdb_exclude_from_next_amount_coin: ArrayList<Double>
        get() = getValue("mdb_exclude_from_next_amount_coin", arrayListOf())

    //表示单次订单金额的最大总金额数
    @JvmStatic
    val mdb_set_cash_off_over_max_amount: String
        get() = getValue("mdb_set_cash_off_over_max_amount", "")

    //表示是否启动支持掉电余额记忆功能，默认支持
    @JvmStatic
    val mdb_save_balance: Boolean
        get() = getValue("mdb_save_balance", true)

    //表示针对现金订单是否手动退款模式
    @JvmStatic
    val mdb_set_manual_refund: Boolean
        get() = getValue("mdb_set_manual_refund", true)

    //表示是否预检查现金找零
    @JvmStatic
    val mdb_set_prechk_refund: Boolean=false
//        get() = getValue("mdb_set_prechk_refund", true)

    //现金找零不足时取消订单
    @JvmStatic
    val mdb_notenough_cancel: Boolean=false
//        get() = getValue("mdb_notenough_cancel", true)

    //现金找零不足时拒收纸币
    @JvmStatic
    val mdb_set_bill_off_on_low_coin: Boolean
        get() = getValue("mdb_set_bill_off_on_low_coin", false)

    //启动纸币器
    @JvmStatic
    val mdb_set_enable_bill: Boolean
        get() = getValue("mdb_set_enable_bill", true)

    //启动硬币器
    @JvmStatic
    val mdb_set_enable_coin: Boolean
        get() = getValue("mdb_set_enable_coin", true)

    //启动管道预警
    @JvmStatic
    val mdb_set_enable_tube: Boolean
        get() = getValue("mdb_set_enable_tube", false)

    @JvmStatic
    val mdb_set_tube_count_coin: String?
        get() {
            val map = getMap()
            val mdb_set_tube_count_coin = map?.get("mdb_set_tube_count_coin")
            if (mdb_set_tube_count_coin != null) {
                val mdb_set_tube_count_coin_json = MdbUtil.anyToJson(mdb_set_tube_count_coin)
                if (true) {
                    return mdb_set_tube_count_coin_json
                }
//                val mdbTubesBean = MdbUtil.jsonToAny<MdbTubesBean>(mdb_set_tube_count_coin_json, MdbTubesBean::class.java)
//                mdbTubesBean?.tubes?.forEach {
//                    if (TextUtils.equals(it.tube_count, "0")) {
//                        it.tube_count = "1"
//                    }
//                    if (TextUtils.equals(it.low_level, "0")) {
//                        it.low_level = "1"
//                    }
//                }
//                if (mdbTubesBean != null) {
//                    return MdbUtil.anyToJson(mdbTubesBean)
//                }
            }
            return null
        }
}